package model;

import java.util.*;

import dao.UserManager;

public class User {
	private static final int MAX_ITEMS = 10;

	private String username;
	private String password1;
	private String password2;
	private String name;
	private String surname;
	private String email;
	private String workplace;
	private boolean isAdmin;
	private int maxItems;
	private HashMap<String, String> errors;

	public User() {
		username = password1 = password2 = name = "";
		surname = email = workplace = "";
		isAdmin = false;
		maxItems = MAX_ITEMS;
		errors = new HashMap<String, String>();
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword1() {
		return password1;
	}

	public void setPassword1(String password1) {
		this.password1 = password1;
	}

	public String getPassword2() {
		return password2;
	}

	public void setPassword2(String password2) {
		this.password2 = password2;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSurname() {
		return surname;
	}

	public void setSurname(String surname) {
		this.surname = surname;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getWorkplace() {
		return workplace;
	}

	public void setWorkplace(String workplace) {
		this.workplace = workplace;
	}

	public boolean isAdmin() {
		return isAdmin;
	}

	public void setAdmin(boolean isAdmin) {
		this.isAdmin = isAdmin;
	}

	public int getMaxItems() {
		return maxItems;
	}

	public void setMaxItems(int maxItems) {
		this.maxItems = maxItems;
	}

	public String getError(String field) {
		String error = errors.get(field);
		return error != null ? error : "";
	}

	public boolean validate(boolean isNew) {
		validateRequiredField("username", username);
		validateRequiredField("name", name);
		validateRequiredField("surname", surname);
		validateRequiredField("email", email);
		if (isNew) {
			if (validateLength("username", username)) {
				userExists();
			}
			validatePassword();
		}
		validateEmail();
		return errors.isEmpty();
	}

	public boolean validatePassword() {
		validateRequiredField("password1", password1);
		validateLength("password1", password1);
		if (!password1.equals("") && !password1.equals(password2)) {
			errors.put("password2", "Passwords do not match");
		}
		return errors.isEmpty();
	}

	private void validateRequiredField(String name, String field) {
		if (field == null || field.equals("")) {
			errors.put(name, name + " cannot be empty");
		}
	}

	private boolean validateLength(String name, String field) {
		if (field.length() < 6) {
			errors.put(name, name + " should be at least 6 characters long");
			return false;
		}
		return true;
	}

	private void userExists() {
		boolean exists = UserManager.containsUser(username);
		if (exists) {
			errors.put("username", "A user with that username already exists");
		}
	}

	private void validateEmail() {
		// TODO: whether the user has entered a valid e-mail address
	}

	public void clearErrors() {
		errors.clear();
	}
}